﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;


using PlanningGIS.ArcGIS;
using PlanningGIS.Util.Log;
using PlanningGIS.Engine.Controls;

namespace PlanningGIS.DataCheck.model
{
    /// <summary>
    /// 山塘名称检查（由于大的山塘与水库图形上很类似，所以有可能将山塘面的名称赋为 “***水库”，对该种错误应该进行检查）
    /// </summary>
    public class PoolNameCheck : Check
    {
        public int Check(string errType, IFeatureLayer pFeatLyr, IGeometry pGeometry, string whereClause, string fcodename, string fnamename)
        {
            IFeatureClass pFeatCls = pFeatLyr.FeatureClass;

            int idxFcode = pFeatCls.FindField(fcodename);

            if (idxFcode == -1)
            {
               LogHelper.WriteErrLog("[" + pFeatLyr.Name + "图层中未找到分类代码[" + fcodename + "]字段，检查失败！");
                return -1;
            }

            int idxFname = pFeatCls.FindField(fnamename);

            if (idxFname == -1)
            {
               LogHelper.WriteErrLog("[" + pFeatLyr.Name + "图层中未找到名称[" + fnamename + "]字段，检查失败！");
                return -1;
            }

            if (pFeatCls.ShapeType != esriGeometryType.esriGeometryPolygon)
            {
               LogHelper.WriteErrLog("[" + pFeatLyr.Name + "图层不是面状图层，检查失败！");
                return -1;
            }

            ISpatialFilter pSpatialFileter = new SpatialFilterClass();
            if (pGeometry != null) pSpatialFileter.Geometry = pGeometry;

            pSpatialFileter.WhereClause = whereClause == "" ? fcodename + " = '240104500'" : whereClause + " and " + fcodename + " = '240104500'";
            
            pSpatialFileter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
            pSpatialFileter.GeometryField = pFeatCls.ShapeFieldName;

            showFakePro(true);
            LogHelper.WriteLog("正在计算需要检查的要素数目...");

            int max = FeatureClassHelper.GetFeatureCountFromLayer(pFeatLyr, pSpatialFileter);// pFeatCls.FeatureCount(pSpatialFileter);
            

            LogHelper.WriteLog("计算完毕。共有" + max + "个要素参与检查。");
            showFakePro(false);

            if (max == 0)
            {
                LogHelper.WriteLog("没有满足检查要求的要素存在。");
                return 0;
            }

            setProcessBarMax(max);

            IFeatureCursor pFeatCur;
            pFeatCur = pFeatLyr.Search(pSpatialFileter, false);

            IFeature pFeature;

            int count = 0;
            int errCount = 0;

            while ((pFeature = pFeatCur.NextFeature()) != null)
            {
                if (IsStop()) break;

                setProcessBarCurrent(count);
                count++;

                if (pFeature.get_Value(idxFname) == DBNull.Value || pFeature.get_Value(idxFname) == null) continue;

                string fnamevalue = pFeature.get_Value(idxFname).ToString();

                if (fnamename.IndexOf("水库") != -1)
                {
                    RecordListHelper.AddRecordItem(errType, pFeatLyr, null, pFeature, null, pFeature.ShapeCopy, "");
                    errCount++;
                }
            }

            finishPro.Invoke();

            DataCheckUtility.ReleaseComObject(pFeature);
            DataCheckUtility.ReleaseComObject(pFeatCur);

            LogHelper.WriteLog("山塘名称异常检查结束。共发现" + errCount + "处错误！\n");

            return errCount;

        }
    }
}
